/**
 * 
 */
package com.wolfpire.proms.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.wolfpire.proms.dao.ProjectIndexDao;
import com.wolfpire.proms.dao.ProjectIndexRoleRelationDao;
import com.wolfpire.proms.model.ProjectIndex;
import com.wolfpire.proms.model.ProjectIndexRoleRelation;
import com.wolfpire.proms.service.ProjectIndexService;
import com.wolfpire.system.common.Constants;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.IBaseHibernateDao;
import com.wolfpire.system.common.base.service.impl.BaseHibernateService;

/**
 * @author lihd
 *
 */

@Service("projectIndexService")
public class ProjectIndexServiceImpl extends
		BaseHibernateService<ProjectIndex, Long> implements ProjectIndexService {
	
	@Resource private ProjectIndexDao projectIndexDao;
	
	@Resource private ProjectIndexRoleRelationDao projectIndexRoleRelationDao;

	@Override
	protected IBaseHibernateDao<ProjectIndex, Long> getEntityDao() {
		return this.projectIndexDao;
	}
	
	@Override
	public List<ProjectIndex> list(ProjectIndex projectIndex) {
		return this.projectIndexDao.list(projectIndex);
	}

	@Override
	public Page<ProjectIndex> setPageDataList(Page<ProjectIndex> page, ProjectIndex filterProjectIndex) {
		return this.projectIndexDao.findProjectIndexs(page, filterProjectIndex);
	}

	@Override
	public void del(Long id) {
		ProjectIndex projectIndex = this.projectIndexDao.get(id);
		if (null != projectIndex) {
			projectIndex.setDelFlag(Constants.DELETE_FLAG);
			this.projectIndexDao.saveOrUpdate(projectIndex);
			Long parentId = projectIndex.getParentId();
			if (null != parentId) {
				List<ProjectIndex> children = this.findChildRen(parentId);
				if (CollectionUtils.isEmpty(children)) {
					ProjectIndex parentProjectIndex = this.projectIndexDao.get(parentId);
					parentProjectIndex.setLeaf(true);
					this.projectIndexDao.save(parentProjectIndex);
				}
			}
		}
	}

	@Override
	public void saveOrUpdate(ProjectIndex projectIndex) {
		Long parentId = projectIndex.getParentId();
		if (null != parentId) {
			//判断是否存在父组织，如果存在父组织，将其父组织设置为非叶子节点
			ProjectIndex parentProjectIndex = this.projectIndexDao.get(parentId);
			parentProjectIndex.setLeaf(false);
			this.projectIndexDao.save(parentProjectIndex);
		}
		if (null != projectIndex.getId()) {
			List<ProjectIndex> children = this.findChildRen(projectIndex.getId());
			if (!CollectionUtils.isEmpty(children)) {
				//判断是否存在子节点，如果存在，将其设置为非叶子节点
				projectIndex.setLeaf(false);
			}
		}
		this.projectIndexDao.saveOrUpdate(projectIndex);
		Long[] roleIds = projectIndex.getRoleIds();
		this.projectIndexRoleRelationDao.relate(projectIndex.getId(), roleIds);
	}

	public List<ProjectIndex> findChildRen(Long id) {
		ProjectIndex projectIndex = new ProjectIndex();
		projectIndex.setParentId(id);
		return this.projectIndexDao.list(projectIndex);
	}

	@Override
	public ProjectIndex getRoleInfoProjectIndex(Long id) {
		ProjectIndex projectIndex = this.projectIndexDao.get(id);
		List<ProjectIndexRoleRelation> pirs = this.projectIndexRoleRelationDao.getPiRRelation(projectIndex.getId());
		if (CollectionUtils.isEmpty(pirs)) {
			return projectIndex;
		}
		List<Long> rolesId = new ArrayList<Long>();
		for (ProjectIndexRoleRelation pir : pirs) {
			rolesId.add(pir.getRoleId());
		}
		projectIndex.setRoleIds(rolesId.toArray(new Long[rolesId.size()]));
//		projectIndex.setRoleIdsStr(StringUtils.join(projectIndex.getRoleIds(), ","));
		return projectIndex;
	}
	
}
